package cn.lili.modules.promotion.serviceimpl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.extra.cglib.CglibUtil;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.promotion.entity.dos.BoxMhgoodsTag;
import cn.lili.modules.promotion.entity.dto.search.BoxSearchParams;
import cn.lili.modules.promotion.entity.vos.BoxMhgoodsTagVO;
import cn.lili.modules.promotion.mapper.BoxMhgoodsTagMapper;
import cn.lili.modules.promotion.service.BoxMhgoodsTagService;
import cn.lili.modules.system.client.DictionaryClient;
import cn.lili.modules.system.constants.DictConstant;
import cn.lili.modules.system.entity.dos.Dictionary;
import cn.lili.mybatis.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author asus
* @description 针对表【box_goods(商城商品表)】的数据库操作Service实现
* @createDate 2023-11-21 10:26:25
*/
@Service
@AllArgsConstructor
@Slf4j
public class BoxMhgoodsTagServiceImpl extends ServiceImpl<BoxMhgoodsTagMapper, BoxMhgoodsTag> implements BoxMhgoodsTagService {

    private DictionaryClient dictionaryClient;

    @Override
    public ResultMessage<Object> pageBoxGoods(BoxSearchParams params) {
        Page<BoxMhgoodsTag> page = super.page(PageUtil.initPage(params), this.createLambdaQueryWrapper(params));
        return ResultUtil.data(this.conversion(page));
    }


    @Override
    public ResultMessage<Object> addBoxGoods(BoxMhgoodsTagVO boxGoodsVO) {
        boxGoodsVO.setId(null);
        return ResultUtil.data(super.save(boxGoodsVO));
    }

    @Override
    public ResultMessage<Object> updateBoxGoods(BoxMhgoodsTagVO boxMhgoodsTag) {
        return ResultUtil.data(super.updateById(boxMhgoodsTag));
    }

    /**
     * 构建搜索条件
     * @param params 参数
     * @return mybatis条件
     */
    private LambdaQueryWrapper<BoxMhgoodsTag> createLambdaQueryWrapper(BoxSearchParams params) {
        LambdaQueryWrapper<BoxMhgoodsTag> boxOptionLambdaQueryWrapper = new LambdaQueryWrapper<>();

        boxOptionLambdaQueryWrapper.eq(BoxMhgoodsTag::getDeleteFlag, Boolean.FALSE);
        boxOptionLambdaQueryWrapper.orderByDesc(BoxMhgoodsTag::getCreateTime);
        return boxOptionLambdaQueryWrapper;
    }

    /**
     * 对象转换
     */
    private Page<BoxMhgoodsTagVO> conversion(Page<BoxMhgoodsTag> paramsPage) {
        //复制page对象
        if (CollectionUtil.isEmpty(paramsPage.getRecords())) {
            return new Page<>();
        }
        Page<BoxMhgoodsTagVO> resultPage = new Page<>();
        BeanUtils.copyProperties(paramsPage, resultPage);
        List<BoxMhgoodsTagVO> voList = CglibUtil.copyList(paramsPage.getRecords(), BoxMhgoodsTagVO::new);
        resultPage.setRecords(voList);

        //字典转换
        List<Dictionary> typeList = dictionaryClient.getListByCode(DictConstant.TAG_NAME);
        for (BoxMhgoodsTagVO boxMhgoodsTagVO : resultPage.getRecords()) {
            if (CollectionUtil.isNotEmpty(typeList)) {
                for (Dictionary dictionary : typeList) {
                    if (dictionary.getValue().equals(String.valueOf(boxMhgoodsTagVO.getTag()))) {
                        boxMhgoodsTagVO.setTagName(dictionary.getValueName());
                    }
                }
            }
        }

        return resultPage;
    }

}




